0
我允許用戶上傳文件到imgur託管並獲取鏈接。一切似乎都在正常工作。但是我正在使用圖片上傳(Filepath) - JSP
String itemName = item.getName();
它在Internet Explorer和Eclipse的瀏覽器中完美工作。但是,當涉及到firefox和chrome時,由於瀏覽器的安全性,只有文件名在輸入字段中被檢索到。 解決方法是什麼讓它起作用?
我的代碼: -
private void processlist(HttpServletRequest request,
HttpServletResponse response) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
} else {
try {
String itemName = item.getName();
BufferedImage img = null;
try {
img = ImageIO.read(new File(itemName));
} catch (IOException e) {
}
String IMGUR_POST_URI = "https://api.imgur.com/3/upload";
String IMGUR_API_KEY = "mykeyyyyyyyyyyyy";
String projectname = "";
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("Writing image...");
ImageIO.write(img, "png", baos);
URL url = new URL(IMGUR_POST_URI);
System.out.println("Encoding...");
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder
.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "="
+ URLEncoder.encode(IMGUR_API_KEY, "UTF-8");
System.out.println("Connecting...");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
System.out.println("Sending data...");
wr.write(data);
wr.flush();
System.out.println("Finished.");
// just display the raw response
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject jObject = new JSONObject(line);
JSONObject data1 = jObject.getJSONObject("data");
projectname = data1.getString("link");
if (flagAccessed == false) {
img1 = projectname;
} else {
img2 = projectname;
}
System.out.print(projectname);
我的JSP代碼是一個簡單的形式: -
<form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1" id="form1" style="text-align:center;display:inline;">
<table border="1" style="text-align:center;">
<td><center>
Select image1: <input name="file" type="file" id="file" class="btn btn-dark btn-lg12">
</center></td>
</tr>
<tr>
<td><center>
Select image2: <input name="file" type="file" id="file" class="btn btn-dark btn-lg12">
</center></td>
</tr>
<tr>
<td align="center" style="margin-top: 5px;"></td>
</tr>
</table>
<br>
<br>
<input type="submit" name="Submit" value="Submit files" class="btn btn-dark btn-lg"/>
</form>
我想確保我理解你的問題。你是說在IE中'items'是一個FileItem對象列表,而在Firefox/Chrome中它是一個字符串列表?或者文件內容不在這些瀏覽器中上傳? – LAROmega
@LAROmega:在IE中,String itemName = item.getName();返回...「C:\ path \ filename.jpg」...總之,我想說的是它返回正確的文件路徑。所以我的代碼工作。在Firefox和Chrome中,String itemName = item.getName();它只返回像filename.jpg這樣的文件名,因此當我試圖將它發送給imgur時,我的img = null。 – starry
我會說IE是在這裏不好。沒有理由服務器需要知道客戶端文件的完整路徑。但修復應該很容易。 if(item.getName()!= null && item.getName()。contains(「\\ /」)'然後解析它,否則就使用item.getName()。編輯:我不是100%,如果你 – LAROmega