I am facing trouble in integrating my project with dropbox I use Dropbox For
upload file here I am able to upload the file by giving complete Path Of file. but I want to upload file by selecting or Brows from the system and upload to my dropbox Here my code is Like Static For Uploading the file by giving complete file path for upload now I want to upload file by selecting from disck here I use this code for selecting the file but i dont know how to pass this selected file as input for FileInputStream in my DbxUpload class
<body>
<a>Select to Upload</a><br><br>
Select file: <br />
<form action="DbxUpload" method="Post" enctype="multipart/form-data">
<input type="file" name="file" size="70" />
<br />
<input type="submit" value="Upload File" />
Here my DbxUpload Class code that iam using
import com.dropbox.core.*;
import java.io.*;
public class DbxUpload
{
private static final String ACCESS_TOKEN = "XXXXXXXXXXXXXXX";
public static void main(String args[]) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
// Upload "test.txt" to Dropbox
try (InputStream in = new FileInputStream("D:/RUNNING.txt")) {
FileMetadata metadata = client.files().uploadBuilder("/RUNNING.txt")
.uploadAndFinish(in);
}
}
}
Please Help me Thanks in Advance
Using web File browser, this is the entry point https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/web-file-browser/src/main/java/com/dropbox/core/examples/web_file_browser/Main.java where the user can start browse and upload a file to drop box api using Jetty application (uses Jetty server and servlet in the program to support file upload to drop box)
Ref :
https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/web-file-browser/src/main/java/com/dropbox/core/examples/web_file_browser
Related
I created a resource: /accounts/{accountId} which uses classes AccountServerResource.class, AccountPage.class and template accountPage.ftl.
Just for testing purpose I created a very simple template containing just one string:
<h1>Hello world</h1>
The page localhost:8111/accounts/21 is displayed correctly.
Now I want to go further and to add some more information to the resource. What I tried to do first, was adding an image to the template:
<h1>Hello, world</h1> <img src="img/user21.jpg">
But this time the image is not displayed. I have an error: the resource localhost:8111/accounts/21/img/user21.jpg is not found. The folder img is stored in the folder containing all *.class files and *.ftl files
How can I expose the image on my template page?
public class TestStaticFile {
public static void main(String[] args) {
Component component = new Component();
Application application = new Application() {
#Override
public Restlet createInboundRoot() {
Directory dir = new Directory(getContext(), "file:///d:/test");
dir.setListingAllowed(true);
return dir;
}
};
component.getServers().add(new Server(Protocol.HTTP, 8888));
component.getClients().add(Protocol.FILE);
component.getDefaultHost().attach(application);
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
If the path of the image is D:/test/test.jpg, now you can access with the url:http://127.0.0.1:8888/test.jpg.
You can refer to the link of the image you want to display in img tag. Restlet supports static files. Save the image in the specified folder and then refer to it in your code.
The Static files setction in restlet user guide may help.
I'm uploading an Excel Workbook to a directory which is outside the application context (for backup purposes) and saving its path into the database.
Now I need to download that file and I'm trying to use Primefaces Download, but I'm getting a null file.
Here is my code (much like Primefaces Showcase Download section):
Bean:
private StreamedContent file;
public void download(Arquivo arquivo) throws IOException {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance()
.getExternalContext().getContext()).getResourceAsStream(arquivo.getNomeArquivo());
file = new DefaultStreamedContent(stream);
}
View:
<h:commandLink id="btnDownload" title="Download Arquivo"
actionListener="#{arquivoBean.download(obj)}">
<p:fileDownload value="#{arquivoBean.file}" />
</h:commandLink>
Basically I need to pass an external path to InputStream instead of FacesContext.
From what I see my problem is that I'm passing my application context to the InputStream, appending the path in the argument of getResourceAsStream which, of course, is not found.
I'm new to this FileDownload thing. Thanks in advance!
If it can still be useful :
StreamedContent streamToReturn = DefaultStreamedContent.builder().name(FILE_NAME).contentType( FacesContext.getCurrentInstance().getExternalContext()
.getMimeType(FILE_PATH))
.stream(() -> {
try {
return new FileInputStream(FILE_PATH);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}).build();
I am trying to use the NanoHttpd library to upload files to my Android server by using a POST request from a form. I do receive the POST request on the server side. My question is how to retrieve the file from the POST request so I can save it somewhere in the device memory.
the form :
<form action='?' method='post' enctype='multipart/form-data'>
<input type='file' name='file' />`enter code here`
<input type='submit'name='submit' value='Upload'/>
</form>
the serve method :
#Override
public Response serve (IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
if (Method.POST.equals(method)) {
//get file from POST and save it in the device memory
}
...
}
Last night i found solution it's very easy to upload file....
First you have manage 'TempFileManager'. It's handle temp file directory so it' create file and delete automatically after your work finish like copy,read,edit,move etc...
server.setTempFileManagerFactory(new ExampleManagerFactory());
So now you not have manage temp file it's work automatically...
Manage post request like this
private Response POST(IHTTPSession) {
try {
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);
Set<String> keys = files.keySet();
for(String key: keys){
String name = key;
String loaction = files.get(key);
File tempfile = new File(loaction);
Files.copy(tempfile.toPath(), new File("destinamtio_path"+name).toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException | ResponseException e) {
System.out.println("i am error file upload post ");
e.printStackTrace();
}
return createResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, "ok i am ");
}
I need to access file system or path a txt file of windows from solaris server. I have a deployment .war into server weblogic solaris, but I don't ability for get out txt file from server to client, in this case windows system or whatever system.
The access to txt files is from,
<input type="file" name="filename" />
I need read the file from client but I'm having FileNotFoundException
PLEASE HELP ME
Your Spring MVC app running on your server does not access the original file on the client's machine (otherwise websites could do bad things to your computer) - the browser sends a copy of the file over the wire to your controller.
Here is a snippet of code I've used to copy the uploaded file to the server's file system:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadFile(
HttpServletResponse response,
#RequestParam(value="filename", required=true) MultipartFile multipartFile,
Model model) throws Exception {
if (!multipartFile.isEmpty()) {
String originalName = multipartFile.getOriginalFilename();
final String baseTempPath = System.getProperty("java.io.tmpdir"); // use System temp directory
String filePath = baseTempPath + File.separator + originalName;
File dest = new File(filePath);
try {
multipartFile.transferTo(dest); // save the file
} catch (Exception e) {
logger.error("Error reading upload: " + e.getMessage(), e);
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "File uploaded failed: " + originalName);
}
}
}
I want to upload a file in struts1 application.
Currently the implementation is using File, like this:
<html:file property="upload"/>
But this does not allow to upload file if app is accessed from remote machine as this widget passes only the name of the file instead the whole file.
using only <html:file property="upload" /> will not make your application to upload a file.
to support upload functionality,your form must have enctype="multipart/form-data"
<html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
File : <html:file property="upload" />
<br/`>
<html:submit />
</html:form`>
and in action get file from your form bean and manipulate it as follows
YourForm uploadForm = (YourForm) form;
FileOutputStream outputStream = null;
FormFile file = null;
try {
file = uploadForm.getFile();
String path = getServlet().getServletContext().getRealPath("")+"/"+file.getFileName();
outputStream = new FileOutputStream(new File(path));
outputStream.write(file.getFileData());
}
finally {
if (outputStream != null) {
outputStream.close();
}
}